package com.teamblueplus.brush;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.EmbossMaskFilter;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.util.Log;

public class PaintBrush {
		public Paint mPaint;
		public MaskFilter mEmboss;
		public MaskFilter mBlur;
	  
	    private static final float MINP = 0.25f;
	    private static final float MAXP = 0.75f;

	 //dont need this
	 //   public Bitmap mBitmap;
	    private Canvas mCanvas;
	    public Path mPath;
	    public Paint mBitmapPaint;
	    
	    private float mX, mY;
	    private static final float TOUCH_TOLERANCE = 4;
	    
	    
	    public List<DrawingPath> brushStack;
	    public DrawingPath currentDrawingPath;
	    private DrawingPath previewDrawingPath;

	  
	  public PaintBrush(){
		
		  mPath = new Path();
	      mBitmapPaint = new Paint(Paint.DITHER_FLAG);
	     
	      mPaint = new Paint();
	      mPaint.setAntiAlias(true);
	      mPaint.setDither(true);
	      mPaint.setColor(0xFFFF0000);
	      mPaint.setStyle(Paint.Style.STROKE);
	      mPaint.setStrokeJoin(Paint.Join.ROUND);
	      mPaint.setStrokeCap(Paint.Cap.ROUND);
	      mPaint.setStrokeWidth(6);

	      mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);

	      mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
	      
	      brushStack = Collections.synchronizedList(new ArrayList<DrawingPath>());
	      
	      currentDrawingPath = new DrawingPath();
          currentDrawingPath.paint = mPaint;
          currentDrawingPath.path = new Path();
	      
	  }
	  
	  
	  public void colorChanged(int color) {
		    mPaint.setColor(color);
	  }
	  
	  public void touch_start(float x, float y) {
		  
		  Log.d("###", "touch start");
		  currentDrawingPath = new DrawingPath();
          currentDrawingPath.paint = mPaint;
          currentDrawingPath.path = new Path();
          
          currentDrawingPath.path.reset();
          currentDrawingPath.path.moveTo(x, y);
          mX=x;
          mY=y;
          
//	      mPath.reset();
//	      mPath.moveTo(x, y);
//	      mX = x;
//	      mY = y;
	    }

	    public void touch_move(float x, float y) {
	      float dx = Math.abs(x - mX);
	      float dy = Math.abs(y - mY);
	      if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
	    	  
	        currentDrawingPath.path.quadTo(mX, mY, (x + mX) / 2, (y + mY) /2);
	       // mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
	        mX = x;
	        mY = y;
	      }
	    }

	    public void touch_up() {
	     currentDrawingPath.path.lineTo(mX, mY);
	     
	     this.brushStack.add(DrawingPath.newInstance(currentDrawingPath.path, currentDrawingPath.paint));

	     currentDrawingPath.path.reset();
	     //currentDrawingPath.path.reset();
	     // mPath.lineTo(mX, mY);
	      // commit the path to our offscreen 
	      //mCanvas.drawPath(mPath, mPaint);
	      
	      // kill this so we don't double draw
	      //mPath.reset();
	    }
	    
	    public void executeAll( Canvas canvas, Handler doneHandler){
	        if( brushStack != null ){
	            synchronized( brushStack ) {
	      
	                final Iterator i = brushStack.iterator();
	                
	                while ( i.hasNext() ){
	                    final DrawingPath drawingPath = (DrawingPath) i.next();
	                    drawingPath.draw( canvas );
	                    //doneHandler.sendEmptyMessage(1);
	                }
	            }
	        }
	    }
	    
	    public void undoBrushStroke(){
	    	int lastStroke = this.brushStack.size();
	    	this.brushStack.remove(lastStroke-1);
	    }
}


